package com.gaofei.loans.job;

import cn.hutool.core.util.RandomUtil;
import com.gaofei.loans.domain.Loans;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zhenzi.sms.ZhenziSmsClient;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class YueKaoJob {

    @Autowired
    HistoryService historyService;

    @XxlJob("yuekaoJobHandler")
    public void yuekaoJobHandler() throws Exception {

        //1.遍历一次贷款申请的列表
        //从数据库查询所有的贷款申请表
        List<Loans> dbLoans = new ArrayList<>();
        Loans loans1 = new Loans(1,"张大",250000.0,30,null,null,1+"");//模拟从数据库查询的数据
        Loans loans2 = new Loans(2,"zhangsan",300000.0,10,null,null,1+"");//模拟从数据库查询的数据
        Loans loans3 = new Loans(3,"gaofei",100000.0,30,null,"http://localhost:9988/71231518-1eurekawmv/1.eureka高可用搭建.wmv",1+"");//模拟从数据库查询的数据
        dbLoans.add(loans1);
        dbLoans.add(loans2);
        dbLoans.add(loans3);

        dbLoans.forEach(loan->{
        //2.如果存在没有审核的申请，给对应负责人发送短信：您由XX条贷款申请需要审批
            if(loan.getStatus().equals(1+"")){
                //没有审核,查询待办任务的负责人
                HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().processInstanceBusinessKey(loan.getId() + "").singleResult();
                //获取负责人
                String assignee = historicTaskInstance.getAssignee();
                //根据负责人查询他的手机号,发送短信
                ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "112696", "5186e0804865a");
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("number", "13261631306");
                params.put("templateId", "10934");
                String[] templateParams = new String[2];
                int i = RandomUtil.randomInt(1000, 9999);


                templateParams[0] = i+"";
                templateParams[1] = "5分钟";
                params.put("templateParams", templateParams);
                try {
                    String result = client.send(params);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

            }
        });
    }
}
